package com.fxkj.cloud.drms.ui.emqx;

import com.fxkj.cloud.drms.infra.config.MqttProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * <p>description: 构建EMQX-webClient</p>
 * @author ww
 * @date 2025/3/17 9:53
 */
@Configuration
public class EmqxWebClientConfiguration {

    private final MqttProperties emqxProperties;

    public EmqxWebClientConfiguration(MqttProperties emqxProperties) {
        this.emqxProperties = emqxProperties;
    }

    @Bean
    public EmqxClient emqxClient() {
        WebClient webClient = WebClient.builder()
                .baseUrl("http://" + emqxProperties.getHost() + ":" + emqxProperties.getWeb().getPort())
                .build();
        HttpServiceProxyFactory httpServiceProxyFactory =
                HttpServiceProxyFactory.builderFor(WebClientAdapter.create(webClient))
                        .build();
        return httpServiceProxyFactory.createClient(EmqxClient.class);
    }
}
